package me.obstsalat.guildera;

import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;

public class g_armor_res {
	
	private static guildera plugin;
	
	g_armor_res(guildera pl) {
		plugin = pl;
	}
	
    public boolean onInventoryClose(InventoryCloseEvent event)
    {
		if(!plugin.checkViableWorld(event.getPlayer().getWorld())) {
			return false;
		}
        if (event.getView().getType() == InventoryType.CRAFTING) {
        	ArmorCheck((Player) event.getPlayer());
        }
        return false;
    }
	
	public boolean onPlayerJoin (PlayerJoinEvent event) {
		if(!plugin.checkViableWorld(event.getPlayer().getWorld())) {
			return false;
		}
		ArmorCheck(event.getPlayer());
		return false;
	}
	
	private void ArmorCheck(Player ply) {
		PlayerInventory inv = ply.getInventory();
		for(int t = 1; t <= 4; t++) {
        	ItemStack it = null;
        	if(t==1) {
        		it = inv.getHelmet();
        	} else if(t==2) {
        		it = inv.getChestplate();
        	} else if(t==3) {
        		it = inv.getLeggings();
        	} else if(t==4) {
        		it = inv.getBoots();
        	}
        	int lvl = plugin.config.getInt("startlevel");
        	for(guild g : plugin.guilds) {
        		if(g.hasMember(ply.getName())) {
        			lvl = g.getLevel();
        		}
        	}
        	if(it != null && it.getTypeId() >= 298 && it.getTypeId() <= 301 && lvl < 1) { //leather armor
        		if(inv.firstEmpty() >= 0) {
    				inv.addItem(it);	
    			} else {
    				ply.getWorld().dropItem(ply.getLocation(), it);
    			}
        		if(t==1) {
        			inv.setHelmet(new ItemStack(0));
        		} else if(t==2) {
        			inv.setChestplate(new ItemStack(0));
        		} else if(t==3) {
        			inv.setLeggings(new ItemStack(0));
        		} else if(t==4) {
        			inv.setBoots(new ItemStack(0));
        		}
        		if(lvl == plugin.config.getInt("startlevel")) {
        			ply.sendMessage("You need to be in a guild to wear armor.");
        		} else {
        			ply.sendMessage("Your guild needs to be level 1 if you want to wear this armor.");
        		}
        	} else if(it != null && it.getTypeId() >= 306 && it.getTypeId() <= 309 && lvl < 40) { //iron armor
        		if(inv.firstEmpty() >= 0) {
    				inv.addItem(it);	
    			} else {
    				ply.getWorld().dropItem(ply.getLocation(), it);
    			}
        		if(t==1) {
        			inv.setHelmet(new ItemStack(0));
        		} else if(t==2) {
        			inv.setChestplate(new ItemStack(0));
        		} else if(t==3) {
        			inv.setLeggings(new ItemStack(0));
        		} else if(t==4) {
        			inv.setBoots(new ItemStack(0));
        		}
        		if(lvl == plugin.config.getInt("startlevel")) {
        			ply.sendMessage("You need to be in a guild to wear armor.");
        		} else {
        			ply.sendMessage("Your guild needs to be level 40 if you want to wear this armor.");
        		}
        	} else if(it != null && it.getTypeId() >= 310 && it.getTypeId() <= 313 && lvl < 60) { //diamond armor
        		if(inv.firstEmpty() >= 0) {
    				inv.addItem(it);	
    			} else {
    				ply.getWorld().dropItem(ply.getLocation(), it);
    			}
        		if(t==1) {
        			inv.setHelmet(new ItemStack(0));
        		} else if(t==2) {
        			inv.setChestplate(new ItemStack(0));
        		} else if(t==3) {
        			inv.setLeggings(new ItemStack(0));
        		} else if(t==4) {
        			inv.setBoots(new ItemStack(0));
        		}
        		if(lvl == plugin.config.getInt("startlevel")) {
        			ply.sendMessage("You need to be in a guild to wear armor.");
        		} else {
        			ply.sendMessage("Your guild needs to be level 60 if you want to wear this armor.");
        		}
        	} else if(it != null && it.getTypeId() >= 314 && it.getTypeId() <= 317 && lvl < 45) { //gold armor
        		if(inv.firstEmpty() >= 0) {
    				inv.addItem(it);	
    			} else {
    				ply.getWorld().dropItem(ply.getLocation(), it);
    			}
        		if(t==1) {
        			inv.setHelmet(new ItemStack(0));
        		} else if(t==2) {
        			inv.setChestplate(new ItemStack(0));
        		} else if(t==3) {
        			inv.setLeggings(new ItemStack(0));
        		} else if(t==4) {
        			inv.setBoots(new ItemStack(0));
        		}
        		if(lvl == plugin.config.getInt("startlevel")) {
        			ply.sendMessage("You need to be in a guild to wear armor.");
        		} else {
        			ply.sendMessage("Your guild needs to be level 45 if you want to wear this armor.");
        		}
        	}
        }
	}
}
